﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class TestExerciseController : Controller
    {
        private MvcApplication2Context db = new MvcApplication2Context();

        public ActionResult CheckSide()
        {
            var side = RouteData.Values["side"];
            if (side != null)
            {
                if (side.ToString().ToLower() == "dev")
                {
                    return null;
                }
                if (side.ToString().ToLower() == "tester")
                {
                    return null;
                }
            }

            return RedirectToAction("Index", new RouteValueDictionary(){ {"side", "dev"}});
        }
        //
        // GET: /TestExercise/

        public ActionResult Index()
        {
            var o = CheckSide();
            if (o != null)
                return o;

            var threadmessages = db.UserMessages.Include(t => t.Exercise);
            return View(threadmessages.ToList());
        }

        //
        // GET: /TestExercise/Details/5

        public ActionResult Details(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            return View(threadmessage);
        }

        //
        // GET: /TestExercise/Create

        public ActionResult Create()
        {
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status");
            return View();
        }

        //
        // POST: /TestExercise/Create

        [HttpPost]
        public ActionResult Create(UserMessage threadmessage)
        {
            if (ModelState.IsValid)
            {
                db.UserMessages.Add(threadmessage);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // GET: /TestExercise/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // POST: /TestExercise/Edit/5

        [HttpPost]
        public ActionResult Edit(UserMessage threadmessage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(threadmessage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ExerciseId = new SelectList(db.Exercises, "ExerciseId", "Status", threadmessage.ExerciseId);
            return View(threadmessage);
        }

        //
        // GET: /TestExercise/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            if (threadmessage == null)
            {
                return HttpNotFound();
            }
            return View(threadmessage);
        }

        //
        // POST: /TestExercise/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserMessage threadmessage = db.UserMessages.Find(id);
            db.UserMessages.Remove(threadmessage);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}